/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormConfiguracoesSistema.java
 *
 * Created on 20/03/2012, 22:45:38
 */
package br.com.ufc.view;

import br.com.ufc.model.ConfiguracoesSistema;
import br.com.ufc.model.PadraoVisualEnum;
import br.com.ufc.model.TipoAgendaEnum;
import javax.swing.JOptionPane;

/**
 *
 * @author Paulo
 */
public class FormConfiguracoesSistema extends javax.swing.JDialog {

    private final String METAL = "javax.swing.plaf.metal.MetalLookAndFeel";
    private final String MODIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
    private final String WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

    /** Creates new form FormConfiguracoesSistema */
    public FormConfiguracoesSistema() {
        initComponents();
        inicializarComponentes();
    }

    private void inicializarComponentes() {
        inicializarComboPadraoVisual();
        inicializarComboTipoTarefa();
        inicializarComboBancoDados();
        inicializarTextFieldTamanhoArray();
    }

    private void inicializarPadraoVisual() {
        if (FormAgendaEti.getConfiguracoesSistema() != null) {
            jcbPadraoVisual.setSelectedItem(FormAgendaEti.getConfiguracoesSistema().getPadraoVisual());
        } else {
            jcbPadraoVisual.setSelectedIndex(0);
        }
    }

    private void inicializarTextFieldTamanhoArray() {
        controleComponenteTamanhoArray();
        if (jpTamanhoArray.isVisible()) {
            if (FormAgendaEti.getConfiguracoesSistema() != null) {
                jtfTamanhoArray.setText(String.valueOf(FormAgendaEti.getConfiguracoesSistema().getTamanhoArray()));
            } else {
                jtfTamanhoArray.setText("1");
            }
        }
    }

    private void inicializarComboPadraoVisual() {
        jcbPadraoVisual .removeAllItems();
        for (Object o : PadraoVisualEnum.values()) {
            jcbPadraoVisual.addItem((PadraoVisualEnum) o);
        }
        if (FormAgendaEti.getConfiguracoesSistema() != null) {
            jcbPadraoVisual.setSelectedItem(FormAgendaEti.getConfiguracoesSistema().getPadraoVisual());
        } else {
            jcbPadraoVisual.setSelectedItem(PadraoVisualEnum.WINDOWS);
        }
    }

    private void inicializarComboTipoTarefa() {
        jcbTipoAgenda.removeAllItems();
        for (Object o : TipoAgendaEnum.values()) {
            jcbTipoAgenda.addItem((TipoAgendaEnum) o);
        }
        if (FormAgendaEti.getConfiguracoesSistema() != null) {
            jcbTipoAgenda.setSelectedItem(FormAgendaEti.getConfiguracoesSistema().getTipoAgenda());
        } else {
            jcbTipoAgenda.setSelectedItem(TipoAgendaEnum.DATA_BASE);
        }
    }

    private void inicializarComboBancoDados() {
        jcbBancoDados.removeAllItems();
        jcbBancoDados.addItem("MYSQL");
        jcbBancoDados.addItem("PostgreSQL");
        if (FormAgendaEti.getConfiguracoesSistema() != null) {
            jcbBancoDados.setSelectedItem(FormAgendaEti.getConfiguracoesSistema().getBancoDados());
        } else {
            jcbBancoDados.setSelectedItem("MYSQL");
        }
    }

    private void closeForm() {
        if (validarConfiguracaoSistema()) {
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Você deve configurar o sistema!", "Erro!",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private Boolean validarConfiguracaoSistema() {
        return (FormAgendaEti.getConfiguracoesSistema() != null ? true : false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jlTipoAgenda = new javax.swing.JLabel();
        jcbTipoAgenda = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jlBancoDados = new javax.swing.JLabel();
        jcbBancoDados = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jbSalvar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jpTamanhoArray = new javax.swing.JPanel();
        jlTamanhoArray = new javax.swing.JLabel();
        jtfTamanhoArray = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jcbPadraoVisual = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Configurações");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jlTipoAgenda.setText("Tipo de Agenda:");

        jcbTipoAgenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTipoAgendaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTipoAgenda)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jcbTipoAgenda, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jcbTipoAgenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jlTipoAgenda))
        );

        jlBancoDados.setText("Tipo de Tarefa:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlBancoDados)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jcbBancoDados, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jlBancoDados)
                .addComponent(jcbBancoDados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jbSalvar.setText("Salvar");
        jbSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarActionPerformed(evt);
            }
        });

        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(95, Short.MAX_VALUE)
                .addComponent(jbCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jbSalvar)
                .addComponent(jbCancelar))
        );

        jpTamanhoArray.setPreferredSize(new java.awt.Dimension(264, 31));

        jlTamanhoArray.setText("Tamanho:");

        javax.swing.GroupLayout jpTamanhoArrayLayout = new javax.swing.GroupLayout(jpTamanhoArray);
        jpTamanhoArray.setLayout(jpTamanhoArrayLayout);
        jpTamanhoArrayLayout.setHorizontalGroup(
            jpTamanhoArrayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpTamanhoArrayLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTamanhoArray)
                .addGap(38, 38, 38)
                .addComponent(jtfTamanhoArray, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
        );
        jpTamanhoArrayLayout.setVerticalGroup(
            jpTamanhoArrayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpTamanhoArrayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jlTamanhoArray)
                .addComponent(jtfTamanhoArray, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel4.setPreferredSize(new java.awt.Dimension(264, 31));

        jLabel1.setText("Padrão Visual:");

        jcbPadraoVisual.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Motif", "Windows", "Metal" }));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jcbPadraoVisual, 0, 158, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jcbPadraoVisual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jpTamanhoArray, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpTamanhoArray, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleParent(null);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void controleComponenteTamanhoArray() {
        if ("ARRAY".equals(((TipoAgendaEnum) jcbTipoAgenda.getSelectedItem()).toString())) {
            jpTamanhoArray.setVisible(true);
            jtfTamanhoArray.setText("1");
        } else {
            jpTamanhoArray.setVisible(false);
        }

        /*
        if ("DATA_BASE".equals(((TipoAgendaEnum) jcbTipoAgenda.getSelectedItem()).toString())) {
        jpTamanhoArray.setVisible(false);
        } else {
        jpTamanhoArray.setVisible(true);
        jtfTamanhoArray.setText("1");
        }
         * 
         */
    }



    private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
        boolean configuracaoInicial = false;
        boolean houveAlteracoes = false;
        if (FormAgendaEti.getConfiguracoesSistema() == null) {
            FormAgendaEti.setConfiguracoesSistema(new ConfiguracoesSistema());
            configuracaoInicial = true;
        }

        ConfiguracoesSistema conf = FormAgendaEti.getConfiguracoesSistema();

        //Padrao visual
        if (configuracaoInicial) {
            houveAlteracoes = true;
            FormAgendaEti.getConfiguracoesSistema().setPadraoVisual(PadraoVisualEnum.getPadraoVisual(jcbPadraoVisual.getSelectedItem().toString().toUpperCase()));
        } else {
            if (conf.getPadraoVisual() != PadraoVisualEnum.getPadraoVisual(jcbPadraoVisual.getSelectedItem().toString().toUpperCase())) {
                houveAlteracoes = true;
                FormAgendaEti.getConfiguracoesSistema().setPadraoVisual(PadraoVisualEnum.getPadraoVisual(jcbPadraoVisual.getSelectedItem().toString().toUpperCase()));
            }
        }

        //Tipo de agenda
        if (configuracaoInicial) {
            houveAlteracoes = true;
            FormAgendaEti.getConfiguracoesSistema().setTipoAgenda((TipoAgendaEnum) jcbTipoAgenda.getSelectedItem());
        } else {
            if (conf.getTipoAgenda() != (TipoAgendaEnum) jcbTipoAgenda.getSelectedItem()) {
                houveAlteracoes = true;
                FormAgendaEti.getConfiguracoesSistema().setTipoAgenda((TipoAgendaEnum) jcbTipoAgenda.getSelectedItem());
            }
        }

        //Banco de dados
        if (configuracaoInicial) {
            houveAlteracoes = true;
            FormAgendaEti.getConfiguracoesSistema().setBancoDados(jcbBancoDados.getSelectedItem().toString());
        } else {
            if (!conf.getBancoDados().equalsIgnoreCase(jcbBancoDados.getSelectedItem().toString())) {
                houveAlteracoes = true;
                FormAgendaEti.getConfiguracoesSistema().setBancoDados(jcbBancoDados.getSelectedItem().toString());
            }
        }

        //Tamanho do array
        if ((jtfTamanhoArray.isVisible()) && (!"".equals(jtfTamanhoArray.getText()))) {

            if (configuracaoInicial) {
                houveAlteracoes = true;
                FormAgendaEti.getConfiguracoesSistema().setTamanhoArray(Integer.valueOf(jtfTamanhoArray.getText()));
            } else {
                if (conf.getTamanhoArray() != Integer.valueOf(jtfTamanhoArray.getText())) {
                    houveAlteracoes = true;
                    FormAgendaEti.getConfiguracoesSistema().setTamanhoArray(Integer.valueOf(jtfTamanhoArray.getText()));
                }
            }

        }
        FormAgendaEti.getConfiguracoesSistema().setHouveAlteracoesSistema(houveAlteracoes);
        this.dispose();

    }//GEN-LAST:event_jbSalvarActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        if (FormAgendaEti.getConfiguracoesSistema() == null) {
            JOptionPane.showMessageDialog(this, "Você deve informar as configurações básicas do sistema!", "Aviso!", JOptionPane.INFORMATION_MESSAGE);
        } else {
            this.dispose();
        }
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        closeForm();
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        closeForm();
    }//GEN-LAST:event_formWindowClosing

    private void jcbTipoAgendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTipoAgendaActionPerformed
        controleComponenteTamanhoArray();
    }//GEN-LAST:event_jcbTipoAgendaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbSalvar;
    private javax.swing.JComboBox jcbBancoDados;
    private javax.swing.JComboBox jcbPadraoVisual;
    private javax.swing.JComboBox jcbTipoAgenda;
    private javax.swing.JLabel jlBancoDados;
    private javax.swing.JLabel jlTamanhoArray;
    private javax.swing.JLabel jlTipoAgenda;
    private javax.swing.JPanel jpTamanhoArray;
    private javax.swing.JTextField jtfTamanhoArray;
    // End of variables declaration//GEN-END:variables
}
